
*------------------------------------------------------------------------------*
* EUROPEAN COMMISSION
*
* Additional specs:
*	1. Fallback: GOV_POS in case of non-aligned Commissioner(s) (or ideological data is missing)
*	2. Fallback: Mean if none of the portfolios is present
*
* Version:	2016-10-28
* Changes:	2016-??-??: First release
*------------------------------------------------------------------------------*


use tmp_eu, clear

gen _____EUROPEAN_COMMISSION = .
save tmp_commission, replace



* ----------------------
* Fallback for non-aligned commissioners: replace ideology with GOV_POS

gen filterpos = $ideology if p601==1 & $ideology != . & p625>=1 & p625!=.
replace filterpos = GOV_POS if  p601==1 & $ideology == . & p625>=1 & p625!=. & filterpos == .



* ----------------------
* Standard Positions


* Mean position
gen filter1 = p625 if p601==1 & filterpos != . & p625>=1 & p625!=.	// new sum of commissioners
by p601 g105, sort: egen zs1sum = sum(filter1)

gen filter2 = filterpos * filter1
by p601 g105, sort: egen zs2sum = sum(filter2)	// mean
gen comm_mean=zs2sum/zs1sum
save, replace


* Median position
collapse (median) comm_median=filterpos [fweight = filter1], by(p601 g105)
save zs1, replace
use tmp_commission, clear
merge p601 g105 using zs1, sort uniqusing
drop _merge zs*
sort g101 g105 p101
save, replace


* Min, Max, Range, Unanimity
by p601 g105, sort: egen comm_min=min(filterpos)
by p601 g105, sort: egen comm_max=max(filterpos)
gen comm_range = comm_max - comm_min
gen comm_unanimity = comm_min + (comm_max - comm_min)/2


* President
gen zs7 = filterpos if p601==1 & p631>0 & p631!=.
by p601 g105, sort: egen comm_pres=mean(zs7)
drop zs*



* ----------------------
* Parse portfolio(s) if "portfolio" or "bargaining" is chosen for any time period

if	"$commission1" == "portfolio" | "$commission1" == "bargaining" |	///
	"$commission2" == "portfolio" | "$commission2" == "bargaining" |	///
	"$commission3" == "portfolio" | "$commission3" == "bargaining" |	///
	"$commission4" == "portfolio" | "$commission4" == "bargaining" |	///
	"$commission5" == "portfolio" | "$commission5" == "bargaining" ///
{

	* Portfolio Commissioner(s)
	local minister "$comm_portf"	// parse portfolios
	*di "`minister'"
	tokenize "`minister'", parse(",")
	local minister1 `1'
	local minister2 `3'
	local minister3 `5'
	local minister4 `7'
	local minister5 `9'
	*di "`minister1'"
	*di "`minister2'"
	*di "`minister3'"
	*di "`minister4'"
	*di "`minister5'"


	* Position of 1st commissioner(s)
	gen zs1 = filterpos if p601 == 1 & p625>=1 & p625!=. & `minister1' > 0
	by p601 g105, sort: egen zs2=mean(zs1)

	* Position of 2nd commissioner(s)
	capture: gen zs3 = filterpos if p601 == 1 & p625>=1 & p625!=. & `minister2' > 0
	capture: by p601 g105, sort: egen zs4=mean(zs3)

	* Position of 3rd commissioner(s)
	capture: gen zs5 = filterpos if p601 == 1 & p625>=1 & p625!=. & `minister3' > 0
	capture: by p601 g105, sort: egen zs6=mean(zs5)

	* Position of 4th commissioner(s)
	capture: gen zs7 = filterpos if p601 == 1 & p625>=1 & p625!=. & `minister4' > 0
	capture: by p601 g105, sort: egen zs8=mean(zs7)

	* Position of 5th commissioner(s)
	capture: gen zs9 = filterpos if p601 == 1 & p625>=1 & p625!=. & `minister5' > 0
	capture: by p601 g105, sort: egen zs10=mean(zs9)


	* Fill in comm_pos
	gen comm_portf = .
	replace comm_portf = zs2  if comm_portf == .
	capture: replace comm_portf = zs4  if comm_portf == .
	capture: replace comm_portf = zs6  if comm_portf == .
	capture: replace comm_portf = zs8  if comm_portf == .
	capture: replace comm_portf = zs10 if comm_portf == .
	capture: replace comm_portf = comm_mean if comm_portf == .	// Fallback if no portfolio is present
	drop zs*
}
save, replace



* ----------------------
* Negotiated postion and minimal connected winning coalition

if	"$commission1" == "bargaining" |	///
	"$commission2" == "bargaining" |	///
	"$commission3" == "bargaining" |	///
	"$commission4" == "bargaining" |	///
	"$commission5" == "bargaining" ///
{

	* Negotiated position between president and commissioner(s)
	gen comm_presportf = (comm_pres + comm_portf) / 2

	*filter per country
	by p601 g105 g101, sort: keep if filter1 != .
	drop if p601 == .
	save zs, replace

	* MCWC starting from negotiated position
	gen range = abs(comm_presportf - filterpos)	// estimate range of each commissioner to negotiated position

	by p601 g105, sort: egen maxvotes = sum(filter1)	// count votes and estimate threshold 50%+1
	gen threshold = maxvotes/2
	sort p601 g105 range
	by p601 g105, sort: gen votes = 1 if _n==1
	replace votes = votes[_n-1] + filter1 if p601[_n-1]==p601 & g105[_n-1]==g105

	gen participant = 1 if votes==1	// mark participants
	replace participant = 1 if votes <= threshold
	replace participant = 1 if participant[_n-1]==1 & votes[_n-1] <= threshold

	by p601 g105, sort: egen zs = mean(filterpos) if participant==1	// mean position of 'new' coalition
	by p601 g105, sort: egen comm_barg = max(zs)

	by p601 g105, sort: drop if _n!=_N	// merge with data
	save, replace
	use tmp_commission, clear
	merge p601 g105 using zs, sort uniqusing keep(comm_barg)
	drop _merge
	sort g101 g105 p101

}



* ----------------------
* Model Choice

gen COMM_POS = .
gen european_commission = ""

lab var COMM_POS 		 	"EU: Position of European Commission"
lab var european_commission "Your model choice for the European Commission"


// "Luxembourg compromise", consultation ( -1987q2)
	if 	"$commission1" == "mean" {
		replace COMM_POS = comm_mean					if g105<=tq(1987q2)
		replace european_commission = "$commission1"	if g105<=tq(1987q2)
	}
	else if "$commission1" == "median" {
		replace COMM_POS = comm_median					if g105<=tq(1987q2)
		replace european_commission = "$commission1"	if g105<=tq(1987q2)
	}
	else if "$commission1" == "unanimity" {
		replace COMM_POS = comm_unanimity				if g105<=tq(1987q2)
		replace european_commission = "$commission1"	if g105<=tq(1987q2)
	}
	else if "$commission1" == "portfolio" {
		replace COMM_POS = comm_portf							if g105<=tq(1987q2)
		replace european_commission = "portfolio($comm_portf)"	if g105<=tq(1987q2)
	}
	else if "$commission1" == "bargaining" {
		replace COMM_POS = comm_barg							if g105<=tq(1987q2)
		replace european_commission = "bargaining($comm_portf)"	if g105<=tq(1987q2)
	}


// Single European Act, cooperation (1987q3-1993q3)
	if 	"$commission2" == "mean" {
		replace COMM_POS = comm_mean					if g105 >=tq(1987q3) & g105<=tq(1993q3)
		replace european_commission = "$commission2"	if g105 >=tq(1987q3) & g105<=tq(1993q3)
	}
	else if "$commission2" == "median" {
		replace COMM_POS = comm_median					if g105 >=tq(1987q3) & g105<=tq(1993q3)
		replace european_commission = "$commission2"	if g105 >=tq(1987q3) & g105<=tq(1993q3)
	}
	else if "$commission2" == "unanimity" {
		replace COMM_POS = comm_unanimity				if g105 >=tq(1987q3) & g105<=tq(1993q3)
		replace european_commission = "$commission2"	if g105 >=tq(1987q3) & g105<=tq(1993q3)
	}
	else if "$commission2" == "portfolio" {
		replace COMM_POS = comm_portf							if g105 >=tq(1987q3) & g105<=tq(1993q3)
		replace european_commission = "portfolio($comm_portf)"	if g105 >=tq(1987q3) & g105<=tq(1993q3)
	}
	else if "$commission2" == "bargaining" {
		replace COMM_POS = comm_barg							if g105 >=tq(1987q3) & g105<=tq(1993q3)
		replace european_commission = "bargaining($comm_portf)"	if g105 >=tq(1987q3) & g105<=tq(1993q3)
	}
	

// Maastricht Treaty, co-decision I (1993q4 - 1999q1)
	if 	"$commission3" == "mean" {
		replace COMM_POS = comm_mean					if g105 >=tq(1993q4) & g105<=tq(1999q1)
		replace european_commission = "$commission3"	if g105 >=tq(1993q4) & g105<=tq(1999q1)
	}
	else if "$commission3" == "median" {
		replace COMM_POS = comm_median					if g105 >=tq(1993q4) & g105<=tq(1999q1)
		replace european_commission = "$commission3"	if g105 >=tq(1993q4) & g105<=tq(1999q1)
	}
	else if "$commission3" == "unanimity" {
		replace COMM_POS = comm_unanimity				if g105 >=tq(1993q4) & g105<=tq(1999q1)
		replace european_commission = "$commission3"	if g105 >=tq(1993q4) & g105<=tq(1999q1)
	}
	else if "$commission3" == "portfolio" {
		replace COMM_POS = comm_portf							if g105 >=tq(1993q4) & g105<=tq(1999q1)
		replace european_commission = "portfolio($comm_portf)"	if g105 >=tq(1993q4) & g105<=tq(1999q1)
	}
	else if "$commission3" == "bargaining" {
		replace COMM_POS = comm_barg							if g105 >=tq(1993q4) & g105<=tq(1999q1)
		replace european_commission = "bargaining($comm_portf)"	if g105 >=tq(1993q4) & g105<=tq(1999q1)
	}


// Amsterdam Treaty, co-decision II (1999q2-2009q4)
	if 	"$commission4" == "mean" {
		replace COMM_POS = comm_mean					if g105 >=tq(1999q2) & g105<=tq(2009q4)
		replace european_commission = "$commission4"	if g105 >=tq(1999q2) & g105<=tq(2009q4)
	}
	else if "$commission4" == "median" {
		replace COMM_POS = comm_median					if g105 >=tq(1999q2) & g105<=tq(2009q4)
		replace european_commission = "$commission4"	if g105 >=tq(1999q2) & g105<=tq(2009q4)
	}
	else if "$commission4" == "unanimity" {
		replace COMM_POS = comm_unanimity				if g105 >=tq(1999q2) & g105<=tq(2009q4)
		replace european_commission = "$commission4"	if g105 >=tq(1999q2) & g105<=tq(2009q4)
	}
	else if "$commission4" == "portfolio" {
		replace COMM_POS = comm_portf							if g105 >=tq(1999q2) & g105<=tq(2009q4)
		replace european_commission = "portfolio($comm_portf)"	if g105 >=tq(1999q2) & g105<=tq(2009q4)
	}
	else if "$commission4" == "bargaining" {
		replace COMM_POS = comm_barg							if g105 >=tq(1999q2) & g105<=tq(2009q4)
		replace european_commission = "bargaining($comm_portf)"	if g105 >=tq(1999q2) & g105<=tq(2009q4)
	}


// Lisbon Treaty, co-decision II with rotating presidency (2010q1- )
	if 	"$commission5" == "mean" {
		replace COMM_POS = comm_mean					if g105 >=tq(2010q1)
		replace european_commission = "$commission5"	if g105 >=tq(2010q1)
	}
	else if "$commission5" == "median" {
		replace COMM_POS = comm_median					if g105 >=tq(2010q1)
		replace european_commission = "$commission5"	if g105 >=tq(2010q1)
	}
	else if "$commission5" == "unanimity" {
		replace COMM_POS = comm_unanimity				if g105 >=tq(2010q1)
		replace european_commission = "$commission5"	if g105 >=tq(2010q1)
	}
	else if "$commission5" == "portfolio" {
		replace COMM_POS = comm_portf							if g105 >=tq(2010q1)
		replace european_commission = "portfolio($comm_portf)"	if g105 >=tq(2010q1)
	}
	else if "$commission5" == "bargaining" {
		replace COMM_POS = comm_barg							if g105 >=tq(2010q1)
		replace european_commission = "bargaining($comm_portf)"	if g105 >=tq(2010q1)
	}



* ----------------------
* Label variables

lab var comm_mean				"European Commission: Mean position"
lab var comm_median				"European Commission: Median position"
lab var comm_min				"European Commission: Minimum position"
lab var comm_max				"European Commission: Maximum position"
lab var comm_range				"European Commission: Range inbetween Commission"
lab var comm_unanimity			"European Commission: Position under unanimity rule"
lab var comm_pres				"European Commission: Position of President"
capture: lab var comm_portf		"European Commission: Position of Comm. for selected portf."
capture: lab var comm_presportf	"European Commission: Mean pos. of Pres. and Comm. for selected portf."
capture: lab var comm_barg		"European Commission: Negotiated position of Commission"



* ----------------------
* Save temporary file

keep if filterpos != . // keep only parties with commissioners
sort g105 g101
keep g101-g105 p101 $ideology p621-p648 _____GOVERNMENT_POSITION GOV_POS _____EUROPEAN_COMMISSION-european_commission
drop filter*
save, replace

capture: erase zs1.dta
capture: erase zs.dta
